package com.atguigu.ucenter.service.impl;

import com.atguigu.jwtUtils.JwtUtils;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.mapper.MemberMapper;
import com.atguigu.ucenter.service.MemberService;
import com.atguigu.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author yky
 * @since 2020-07-13
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(Member member) {
        //获取member中的手机号和密码，判断是否为空
        String mobile = member.getMobile();//手机号
        String password = member.getPassword();//密码
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登陆失败，手机号或密码错误");
        }
        //手机号在数据库中查询，判断手机号是否正确
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member memberByMobile = baseMapper.selectOne(wrapper);
        if(memberByMobile == null){
            throw new GuliException(20001,"登陆失败，手机号错误");
        }

        //判断密码是否正确
        //密码加密处理，
        if(!MD5.encrypt(password).equals(memberByMobile.getPassword())){
            throw new GuliException(20001,"登陆失败，密码错误");
        }
        System.out.println(memberByMobile.getPassword());
        //判断用户是否被禁用
        if(memberByMobile.getIsDisabled()){
            throw new GuliException(20001,"登陆失败，该账号被禁用");
        }

        //验证成功后，把信息封装到字符串中
        String token = JwtUtils.getJwtToken(memberByMobile.getId(), memberByMobile.getNickname());

        return token;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        String rcode = registerVo.getCode();
        String rmobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //判空
        if(StringUtils.isEmpty(rcode)||
                StringUtils.isEmpty(rmobile)||
                StringUtils.isEmpty(nickname)||
                StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败，信息不完整");
        }

        //验证验证码
        String code = registerVo.getCode();
        String redisCode = redisTemplate.opsForValue().get(rmobile);
        if(!redisCode.equals(code)){
            throw new GuliException(20001,"验证码错误");
        }

        //验证码正确，查询手机号是否重复
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",rmobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001,"手机号已经存在");
        }

        //封装数据，插入
        Member member = new Member();
        member.setMobile(rmobile);//手机号
        member.setNickname(nickname);//昵称
        member.setPassword(MD5.encrypt(password));//密码
        member.setIsDisabled(false);//不禁用
        //设置默认头像
        member.setAvatar("https://youkaiyang.oss-cn-beijing.aliyuncs.com/2020/07/07/f919d7b981c94b3f80e961dde72ea041file.png");
        int insert = baseMapper.insert(member);
        if(insert <= 0){
            throw new GuliException(20001,"注册失败（插入失败）");
        }
    }

    //通过openid查询
    @Override
    public Member getOpenMemer(String userOpenId) {

        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",userOpenId);
        Member member = baseMapper.selectOne(wrapper);
        return member;

    }

    //获取每天注册人数
    @Override
    public Integer getNumByDay(String day) {

        Integer count =  baseMapper.getNumByDay(day);

        return count;
    }
}
